# coding: utf-8
# author: ZhenbPeng
#   date: 2017/10/17 14:45
import logging
import json
import requests

from settings import Setting
from utils.redis_cache import ProxyStats


logger = logging.getLogger("default")


def get_from_api():
    """取得代理ip list"""

    hosturl = 'http://vtp.daxiangdaili.com/ip/?tid=558736294079813&num=%s&category=2&filter=on' % Setting.THREAD_NUM
    response = requests.get(hosturl)
    if response.status_code == 200:
        ip_list = response.text.split()
        return ip_list
    else:
        return []


def get_from_web():
    pass


def clean_proxyip(ip, port):
    check_url = 'http://httpbin.org/get'
    proxy = {
        "http": "http://%s:%s" % (ip, port)
    }
    try:
        response = requests.get(url=check_url, proxies=proxy, timeout=30)
    except Exception as e:
        return False

    if response.status_code == 200:
        result = json.loads(response.text)
        if result.get("origin", "") == ip:
            return True

    return False


class ProxyGet(object):
    ip_list = []

    def get_proxylist(self):
        self.ip_list = get_from_api()
        logger.info("proxy module get ip_list length=%s" % len(self.ip_list))
        ProxyStats().add("ip_sum", len(self.ip_list))
